/* @flow */

import { parse } from './parser/index'
import { optimize } from './optimizer'
import { generate } from './codegen/index'
import { createCompilerCreator } from './create-compiler'

// `createCompilerCreator` allows creating compilers that use alternative
// parser/optimizer/codegen, e.g the SSR optimizing compiler.
// Here we just export a default compiler using the default parts.
// 柯里化函数，baseCompile 是将 template 编译为 compiled 的核心函数
export const createCompiler = createCompilerCreator(function baseCompile (
  template: string,
  options: CompilerOptions
): CompiledResult {
  // 1. 将 template 编译为 ast（抽象语法树）
  const ast = parse(template.trim(), options)
  // 2. 对抽象语法树进行优化
  if (options.optimize !== false) {
    optimize(ast, options)
  }
  
  // 3. 根据 ast 生成 js 字符串，将生成的字符串放到 code.render 上
  const code = generate(ast, options)
  return {
    ast,
    render: code.render,
    staticRenderFns: code.staticRenderFns
  }
})
